//
//  MuaAPI.m
//  AppMua5
//
//  Created by duong nguyen on 9/14/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "MuaAPI.h"

@implementation MuaAPI
@synthesize parser,productArr,shopArr,request;

-(id)init{
    [super init];
    request=[[ASIHTTPRequest alloc]init];
    self.parser=[[SBJsonParser alloc]init];
    self.productArr=[[NSMutableArray alloc]init];
    self.shopArr=[[NSMutableArray alloc]init];
    return self;
}

#pragma Call request APIs
-(void)grabURLInBackground:(NSString *)urlStr context:(id)context_id{
    NSURL *url=[NSURL URLWithString:urlStr];
    request=[ASIHTTPRequest requestWithURL:url];
    [request setDelegate:context_id];
    [request startAsynchronous];
    
    url=nil;
    [url release];
}

#pragma List Product By Category
// Get product list by category
-(void) getListProductByCategory:(NSInteger)categoryId context:(id)context_id{
    NSMutableString *urlStr=[NSMutableString stringWithFormat:@"%@%@",ROOT_SERVER,GET_LIST_PRODUCT_BY_CATEGOTY];
    [urlStr appendFormat:@"%@%i",@"&categoryId=",categoryId];
    
    [self grabURLInBackground:urlStr context:context_id];
}

// Parse data from response
-(NSMutableArray *) parseListProductByCategory:(NSString *)response{
    NSDictionary *dicObject=[[NSDictionary alloc]init];
    // Remove
    [self.productArr removeAllObjects];
    [self.shopArr removeAllObjects];
    NSDictionary *data=[parser objectWithString:response error:nil];
    
    //NSLog(@"%@",data);
    NSArray *arr=[data objectForKey:@"data"];
    NSArray *arrS=[[data objectForKey:@"shopInfos"]allValues];
    
    // Lay du lieu shop info
    int i;
    for (i=0; i<arrS.count; i++) {
        dicObject=[arrS objectAtIndex:i];
        ShopObject *shop=[[ShopObject alloc]init];
        shop.shopId=[[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"shop_id"]]intValue];
        shop.shopName=[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"shop_name"]];
        shop.shopWebsite=[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"email"]];
        shop.shopPhone=[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"phone"]];
        shop.shopAddress=[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"address_street_number_ward"]];
        [shopArr addObject:shop];
        [shop release];
    }
    
    // Do du lieu cho danh sach san pham.
    for (i=0; i<[arr count]; i++) {
        dicObject=[arr objectAtIndex:i];
        ProductObject *product=[[ProductObject alloc]init];
        product.shop=[[ShopObject alloc]init];
        product.productName=[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"product_name"]];
        product.productImage=[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"product_image"]];
        product.productPrice=[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"product_price"]];
        product.productSummary=[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"product_summary"]];
        product.categoryId=[[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"category_id"]]intValue];
        product.shopId=[[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"shop_id"]]intValue];
        product.userId=[[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"user_id"]]intValue];
        product.productId=[[NSString stringWithFormat:@"%@",[dicObject objectForKey:@"product_id"]]intValue];
        
        for (int j=0; j<shopArr.count; j++) {
            if ([[shopArr objectAtIndex:j]shopId]==product.shopId) {
                product.shop=[shopArr objectAtIndex:j];
            }
        }
        [productArr addObject:product];
        [product release];
    }
    
    // Release when finish
    //[data release];
    dicObject=nil;
    data=nil;
    arr=nil;
    arrS=nil;
    [dicObject release];
    [data release];
    [arr release];
    [arrS release];
    return productArr;
}

// Parse list product by shop
-(void) getListProductByShop:(NSInteger)userId shopObject:(ShopObject *)shop context:(id)context_id{
    NSMutableString *urlStr=[NSMutableString stringWithFormat:@"%@%@",ROOT_SERVER,GET_LIST_PRODUCT_BY_SHOP];
    //them tham so userId
    [urlStr appendFormat:@"%@%i",@"&userid=",userId];
    
    [self grabURLInBackground:urlStr context:context_id];
    
    urlStr=nil;
    [urlStr release];
}

-(NSMutableArray *) parseListProductByShop:(NSString *)response shopObject:(ShopObject *)shop{
    
    NSMutableArray *productList=[[NSMutableArray alloc]init];
    NSDictionary *dic=[parser objectWithString:response error:nil];
    NSArray *arr=[dic objectForKey:@"products"];
    
    for (int i=0; i<[arr count]; i++) {
        NSDictionary *object=[arr objectAtIndex:i];
        ProductObject *product=[[[ProductObject alloc]init]autorelease];
        product.categoryId=[[NSString stringWithFormat:@"%@",[object objectForKey:@"category_id"]]intValue];
        product.productImage=[NSString stringWithFormat:@"%@",[object objectForKey:@"product_image"]];
        product.productName=[NSString stringWithFormat:@"%@",[object objectForKey:@"product_name"]];
        product.productPrice=[NSString stringWithFormat:@"%@",[object objectForKey:@"product_price"]];
        product.productSummary=[NSString stringWithFormat:@"%@",[object objectForKey:@"product_summary"]];
        product.shopId=[[NSString stringWithFormat:@"%@",[object objectForKey:@"shop_id"]]intValue];
        product.userId=[[NSString stringWithFormat:@"%@",[object objectForKey:@"user_id"]]intValue];
        product.shop=shop;
        NSLog(@"%@",product.productName);
        [productList addObject:product];
        [product release];
    }
    
    arr=nil;
    dic=nil;
    [arr release];
    [dic release];
    return productList;

}

-(void) getProductInfo:(NSInteger)userId productId:(NSInteger)productId context:(id)context_id{
    NSMutableString *urlStr=[NSMutableString stringWithFormat:@"%@%@",ROOT_SERVER,GET_PRODUCT_INFO];
    [urlStr appendFormat:@"&userId=%i",userId];
    [urlStr appendFormat:@"&productId=%i",productId];
    
    [self grabURLInBackground:urlStr context:context_id];
    urlStr=nil;
    [urlStr release];
}

-(ProductObject *)parseProductInfo:(NSString *)response ShopObject:(ShopObject *)shop{
    
    NSDictionary *data=[parser objectWithString:response error:nil];
    NSLog(@"%@",data);
    return nil;
}
-(void)dealloc{
    [self.parser release];
    [self.request release];
    [self.productArr release];
    [self.shopArr release];
    [super dealloc];
}
@end
